<template>
	<view>
		<template>
			<view class="lee-wrap">
				<view class="lee-menu-wrap">
					<scroll-view scroll-y scroll-with-animation class="lee-tab-view menu-scroll-view bf5"
						:scroll-top="scrollTop" :scroll-into-view="itemId">
						<view v-for="(item,index) in classfiy" :key="index" class="lee-tab-item"
							:class="[current == index ? 'lee-tab-item-active' : '']"
							:style="{color:current == index ?subColor:''}" @click.stop="swichMenu(index,item)">
							<text class="u-line-1">{{item.name}}</text>
						</view>
					</scroll-view>
					<scroll-view :scroll-top="scrollRightTop" scroll-y scroll-with-animation class="right-box"
						@scroll="rightScroll">
						<view class="page-view">
							<view class="class-item" :id="'item' + index" v-for="(item , index) in list" :key="index">
								<!-- <view class="item-title  bs6">
									<text>{{item.goods.category && item.goods.category[index].name}}</text>
								</view> -->
								<slot name="custom" :row="item">
									<view class="item-container">
										<view class=" p-10-0" v-for="(item1, index1) in list" :key="index1">
											<view class="f-s">
												<image class="item-menu-img bs6" :src="item1.goods.logo" mode="">
												</image>
												<!-- <view v-else class="imgBox bs6" style="background-color: #F4F6F8;">
													<view class="cover bs6 cf tac">已下架</view>
													<image class="item-menu-img bs6" :src="item1.icon" mode=""></image>
												</view> -->
												<view class="ml10 f-1 f-y-bt" style="height: 160rpx;">
													<text class="wei6 f18">{{item1.goods.name}}</text>
													<view class="dfbc">
														<view class="cf5" v-if="item1.goods.specSwitch == 0">
															<text class="f12">￥</text>
															<text
																class="f16">{{item1.goods.singleSpec && item1.goods.singleSpec.price}}</text>
														</view>
														<view v-else>
															{{ item1.goods.mixPrice }}~{{ item1.goods.maxPrice }}
														</view>
														<view class="c9 dfa" @click="inventory(item1)">
															<text class="f12 pr5"
																v-if="item1.goods.specSwitch == 0">库存:{{ item1.goods.singleSpec && item1.goods.singleSpec.surplusInventory }}</text>
															<text class="f12 pr5" v-else>库存:详情</text>
															<u-icon name="edit-pen" color="#999" size="18"></u-icon>
														</view>
													</view>
												</view>
											</view>
											<view class="mt10 tar">
												<view class="button_common" @click="$emit('putsxj',item1)"
													v-if="!item1.deleted_at">下架</view>
												<view class="button_common" @click="$emit('putsxj',item1)"
													v-if="item1.deleted_at">上架</view>
											</view>
										</view>
									</view>
								</slot>
							</view>
						</view>
					</scroll-view>
				</view>
			</view>
		</template>
	</view>
</template>

<script>
	export default {
		props: {
			// 数据列表
			list: {
				type: Array,
				default () {
					return []
				}
			},
			classfiy: {
				type: Array,
				default () {
					return []
				}
			}
		},
		data() {
			return {
				subColor: uni.getStorageSync('subject_color'),
				scrollTop: 0, //tab标题的滚动条位置
				oldScrollTop: 0, // tab标题的滚动条旧位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				itemId: '', // 栏目右边scroll-view用于滚动的id
				arr: [], // 储存距离顶部高度的数组
				scrollRightTop: 0, // 右边栏目scroll-view的滚动条高度
				timer: null,
			}
		},
		mounted() {
			// this.getMenuItemTop()
		},
		methods: {
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height; //右边内容高度
						resolve();
					}).exec();
				})
			},
			getMenuItemTop() {
				new Promise(resolve => {
					let selectorQuery = uni.createSelectorQuery().in(this);
					selectorQuery.selectAll('.class-item').boundingClientRect((rects) => {
						if (!rects.length) {
							setTimeout(() => {
								this.getMenuItemTop();
							}, 10);
							return;
						}
						rects.forEach((rect) => {
							this.arr.push(rect.top)
							resolve();
						})
					}).exec()
				})
			},
			// 设置左边菜单的滚动状态
			async leftMenuStatus(index) {
				this.current = index;
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 123 || this.menuItemHeight == 123) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('lee-tab-item', 'menuItemHeight');
				}
				// 将菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 点击左边的栏目切换
			async swichMenu(index, item) {
				this.current = index;
				this.$emit('clickfl', item)
				return
				if (this.arr.length == 0) {
					await this.getMenuItemTop();
				}
				if (index == this.current) return;
				this.scrollRightTop = this.oldScrollTop;
				this.$nextTick(function() {
					this.scrollRightTop = this.arr[index];
					this.current = index;
					this.leftMenuStatus(index);
				})
			},
			// 右边菜单滚动  如果不存在height2，意味着数据循环已经到了最后一个，设置左边菜单为最后一项即可
			async rightScroll(e) {
				return
				this.oldScrollTop = e.detail.scrollTop;
				if (this.arr.length == 0) {
					await this.getMenuItemTop();
				}
				if (this.timer) return;
				if (!this.menuHeight) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
				}
				setTimeout(() => { // 节流
					this.timer = null;
					let scrollHeight = e.detail.scrollTop + 20;
					for (let i = 0; i < this.arr.length; i++) {
						let height1 = this.arr[i];
						let height2 = this.arr[i + 1];
						if (!height2 || scrollHeight >= height1 && scrollHeight < height2) {
							this.leftMenuStatus(i);
							return;
						}
					}
				}, 10)
			},
			inventory(v) {
				console.log('0000');
				this.$emit("changeShow", true, v);
			}
		}
	}
</script>

<style lang="scss" scoped>
	$lee-main-color: #101010;
	$lee-left-color: #00aaff;
	$lee-tab-item-active: #c6ebff;

	.lee-wrap {
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
		height: 100vh;
	}

	.lee-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.lee-tab-view {
		width: 200rpx;
		height: 100%;
	}

	.lee-tab-item {
		height: 110rpx;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}

	.lee-tab-item-active {
		position: relative;
		font-size: 30rpx;
		font-weight: 500;
		background: #fff;
	}

	.lee-tab-view {
		height: 100%;
	}

	.right-box {
		background-color: rgb(250, 250, 250);
	}

	.page-view {
		background-color: #fff;
		padding: 16rpx;
	}

	.class-item {
		margin-bottom: 30rpx;
		padding: 16rpx;
		border-radius: 8rpx;
	}

	.class-item:last-child {
		min-height: 100vh;
	}

	.item-title {
		display: inline-block;
		padding: 16rpx 26rpx;
		font-weight: bold;
	}

	.item-menu-name {
		margin-top: 8rpx;
		font-weight: normal;
		font-size: 24rpx;
		color: $lee-main-color;
	}

	.item-menu-img {
		width: 160rpx;
		height: 160rpx;
	}

	.imgBox {
		position: relative;

		.cover {
			position: absolute;
			top: 0;
			left: 0;
			width: 160rpx;
			height: 160rpx;
			line-height: 160rpx;
			background-color: rgba(#000, 0.5);
		}
	}
</style>